import { _decorator, Component, ERigidBody2DType, EventTouch, Node, ParticleSystem2D, tween, Tween, UITransform, v3, Vec3 } from 'cc';
import { TweenTag } from './Subject';
import { Item } from './Item';
import { PoolManager } from '../../Scripts/Framework/Managers/PoolManager';
import { MigaManager } from './MigaManager';
const { ccclass, property } = _decorator;

@ccclass('Food')
export class Food extends Item {

    @property
    isReady = true;

    canEat = false;

    process: UITransform;

    HandInit(url: string) {
        this.rig.type = ERigidBody2DType.Animated;
        this.rig.enabledContactListener = false;
        this.collider.enabled = false;
        this.Init();
    }

    TouchMove(event: EventTouch) {
        if (this.isTween) return;
        if (MigaManager.Instance.tutorial.active) return;
        super.TouchMove(event);
        if (this.personTarget && !this.isBottle) {
            if (this.personTarget.mouth.children[1].children.length > 0) return;
            if (this.personTarget.mouth.getComponent(UITransform).getBoundingBoxToWorld().intersects(this.uiTransform.getBoundingBoxToWorld())) {
                if (!this.canEat) {
                    this.canEat = true;
                    if (this.isReady && this.canEat) this.personTarget.Mouth(1);
                }
            }
            else {
                this.canEat = false;
                this.personTarget.Mouth(0);
            }
        }
        else if (this.headPerson && this.isBottle) {
            if (this.headPerson.mouth.children[1].children.length > 0) return;
            if (this.headPerson.mouth.getComponent(UITransform).getBoundingBoxToWorld().intersects(this.node.children[1].getComponent(UITransform).getBoundingBoxToWorld())) {
                this.headPerson.Mouth(1);
            }
            else this.headPerson.Mouth(0);
        }
        else {
            this.canEat = false;
            this.personTarget?.Mouth(0);
            this.headPerson?.Mouth(0);
        }
        if (!this.isReady && this.getComponent(UITransform).getBoundingBoxToWorld().intersects(this.process.getBoundingBoxToWorld())) {
            this.isReady = true;
            this.node.children[0].active = true;
        }
    }

    TouchEnd(event: EventTouch) {
        if (this.isTween) return;
        if (!this.isTouch) return;
        if (MigaManager.Instance.tutorial.active) return;
        MigaManager.Instance.edgePoint = null;
        Tween.stopAllByTag(TweenTag.Touch, this.node);
        tween(this.node).tag(TweenTag.Touch)
            .to(0.1, { scale: Vec3.ONE })
            .start();
        if (this.isBottle) {
            Tween.stopAllByTag(TweenTag.Angle);
            tween(this.node).tag(TweenTag.Angle)
                .to(0.2, { angle: 0 })
                .call(() => {
                    if (!this.node.children[0].getComponent(ParticleSystem2D)) this.node.children[0].active = false;
                    this.node.children[0].getComponent(ParticleSystem2D)?.stopSystem();
                })
                .start();
        }
        this.touchUpPos = this.node.getPosition();
        this.isTouch = false;
        if (this.handTarget && !(this.canEat && this.isReady)) this.handPerson.Hand(this);
        if (this.headPerson && this.isBottle) this.headPerson.Mouth(0);
        if (this.canEat && this.isReady && this.headPerson.mouth.children[1].children.length == 0) {
            if (this.personTarget) this.personTarget.Mouth(2);
            else if (this.headPerson) this.headPerson.Mouth(2);
            if (this.handPerson) {
                let i = this.handPerson.handContacts.indexOf(this.handTarget);
                if (this.handPerson.handStates[i] == 1) {
                    let tag = i == 0 ? TweenTag.LeftArm : TweenTag.RightArm;
                    Tween.stopAllByTag(tag, this.handPerson.hands[i]);
                    tween(this.handPerson.hands[i]).tag(tag)
                        .to(0.2, { angle: 0 })
                        .start();
                }
            }
            PoolManager.PutNode(this.node);
        }
        else {
            this.canEat = false;
            if (this.personTarget) this.personTarget.Mouth(0);
            else if (this.headPerson) this.headPerson.Mouth(0);
        }
        if (this.sitTarget && !(this.canEat && this.isReady && this.headPerson.mouth.children[1].children.length == 0)) {
            this.node.setParent(this.sitTarget, true);
            let otherWidth = this.sitTarget.getComponent(UITransform).width;
            let delta = this.sitTarget.getWorldPosition().x - this.node.getWorldPosition().x;
            let pos = v3(this.node.getWorldPosition().x, this.sitTarget.getWorldPosition().y);
            if (otherWidth < this.uiTransform.width) pos.x = this.sitTarget.getWorldPosition().x;
            else if (delta > otherWidth / 2 - this.width / 2) pos.x = this.sitTarget.getWorldPosition().x - otherWidth / 2 + this.width / 2;
            else if (delta < this.width / 2 - otherWidth / 2) pos.x = this.sitTarget.getWorldPosition().x + otherWidth / 2 - this.width / 2;
            Tween.stopAllByTag(TweenTag.FallCollid, this.node);
            tween(this.node).tag(TweenTag.FallCollid)
                .to(0.2, { worldPosition: pos })
                .start();
        }
        else {
            this.Raycast(event.getUILocation());
        }
    }

}
